# coding=utf-8
import os
import time

import allure
import pytest,jsonpath
from common.assert_operator import Pact, string_assert, dict_assert
from common.db import sql
from common.logger import Logger
from common.read_yaml import ReadYaml_By_Root_Dict, ReadYaml
from common.verification import Verification
from common.utils import Utils

string_assert = string_assert() # 字符串校验类
pact = Pact() # 契约校验类
verification = Verification()
data = ReadYaml().get_yaml_data(f'{os.path.abspath(__file__).split("_test.py")[0]}.yml')   # 读取数据

@allure.feature('审批时跳过')  # 测试报告显示测试功能
class Test_07_1_approve_skip():

    # 获取yml文件中[01审批时相同处理人后续步骤审批]的数据参数
    approve_process_params = data['01审批时相同处理人后续步骤审批']
    # 获取yml的title，并作为ids的值传给用例
    ids_list = jsonpath.jsonpath(approve_process_params,"$[*][0]")
    @pytest.mark.parametrize("title, param, expect", approve_process_params,ids=ids_list)
    def test_approve_skip_1(self, user_login, title, param, expect):
        Logger.info("*************** {}开始执行用例 ***************".format(title))
        allure.dynamic.description(f"{param['description']}")  # # 动态解析用例描述在测试报告中展示
        user,process_name = param['user'],param['process_name']
        DBNodeVerify, MessageVerify = '数据库校验', '消息校验'
        contract_format = pact.approve_pact_format()
        main = user_login.goto_Approve_Process(user)    # 登录到审批类实例
        process_instance_resp = main.open_process_instance(process_name)   # 通过流程名称打开流程实例
        process_params = main.get_key_by_process_instance(process_instance_resp,'params')['params']  # 打开流程响应的Params
        proc_id = jsonpath.jsonpath(process_params,'$.proc_id')[0] # 当前流程的proc_id
        node_id = jsonpath.jsonpath(process_params,'$.node_id')[0] # 当前流程的node_id
        package_id = jsonpath.jsonpath(process_params,'$.package_id')[0] # 当前流程的package_id
        response = main.approve_submit(param, process_instance_resp)    # 提交审批
        # 审批后校验
        verification.after_approve(process_name, response,
                                   expect, contract_format, DBNodeVerify, MessageVerify)
        Logger.info("*************** 【{}】审批撤回 ***************".format(process_name))
        resp_body = '撤回请求校验'
        # 使用已审批的节点信息打开流程
        process_instance = main.open_process_instance(node_id=node_id, proc_id=proc_id, package_id=package_id)
        # 撤回审批
        withdraw_resp = main.approval_withdraw(process_instance, param['process_name'], '审批撤回')
        # 撤回后校验
        verification.after_approve(process_name, withdraw_resp, expect, resp_body=resp_body)
        Logger.info("*************** {}结束执行用例 ***************".format(title))

    # 获取yml文件中[02审批时相同处理人后续步骤审批_不跳场景]的数据参数
    approve_process_params = data['02审批时相同处理人后续步骤审批_不跳场景']
    # 获取yml的title，并作为ids的值传给用例
    ids_list = jsonpath.jsonpath(approve_process_params,"$[*][0]")
    @pytest.mark.parametrize("title, param, expect", approve_process_params,ids=ids_list)
    def test_approve_not_skip_2(self, user_login, title, param, expect):
        Logger.info("*************** {}开始执行用例 ***************".format(title))
        allure.dynamic.description(f"{param['description']}")  # # 动态解析用例描述在测试报告中展示
        user,process_name = param['user'],param['process_name']
        DBNodeVerify, MessageVerify = '数据库校验', '消息校验'
        contract_format = pact.approve_pact_format()
        main = user_login.goto_Approve_Process(user)    # 登录到审批类实例
        process_instance_resp = main.open_process_instance(process_name)   # 通过流程名称打开流程实例
        response = main.approve_submit(param, process_instance_resp)    # 提交审批
        # 审批后校验
        verification.after_approve(process_name, response,
                                   expect, contract_format, DBNodeVerify, MessageVerify)
        Logger.info("*************** {}结束执行用例 ***************".format(title))

    # 获取yml文件中[03处理人已审批过]的数据参数
    approve_process_params = data['03处理人已审批过']
    # 获取yml的title，并作为ids的值传给用例
    ids_list = jsonpath.jsonpath(approve_process_params,"$[*][0]")
    @pytest.mark.parametrize("title, param, expect", approve_process_params,ids=ids_list)
    def test_approve_skip_3(self, user_login, title, param, expect):
        Logger.info("*************** {}开始执行用例 ***************".format(title))
        allure.dynamic.description(f"{param['description']}")  # # 动态解析用例描述在测试报告中展示
        user,process_name = param['user'],param['process_name']
        DBNodeVerify, MessageVerify = '数据库校验', '消息校验'
        contract_format = pact.approve_pact_format()
        main = user_login.goto_Approve_Process(user)    # 登录到审批类实例
        process_instance_resp = main.open_process_instance(process_name)   # 通过流程名称打开流程实例
        process_params = main.get_key_by_process_instance(process_instance_resp,'params')['params']  # 打开流程响应的Params
        proc_id = jsonpath.jsonpath(process_params,'$.proc_id')[0] # 当前流程的proc_id
        node_id = jsonpath.jsonpath(process_params,'$.node_id')[0] # 当前流程的node_id
        package_id = jsonpath.jsonpath(process_params,'$.package_id')[0] # 当前流程的package_id
        response = main.approve_submit(param, process_instance_resp)    # 提交审批
        # 审批后校验
        verification.after_approve(process_name, response,
                                   expect, contract_format, DBNodeVerify, MessageVerify)
        Logger.info("*************** 【{}】审批撤回 ***************".format(process_name))
        resp_body = '撤回请求校验'
        # 使用已审批的节点信息打开流程
        process_instance = main.open_process_instance(node_id=node_id, proc_id=proc_id, package_id=package_id)
        # 撤回审批
        withdraw_resp = main.approval_withdraw(process_instance, param['process_name'], '审批撤回')
        # 撤回后校验
        verification.after_approve(process_name, withdraw_resp, expect, resp_body=resp_body)
        Logger.info("*************** {}结束执行用例 ***************".format(title))